package factory;

import java.lang.reflect.Proxy;

public class LogTest {
    public static void main(String[] args) {
        LogService logService=new LogServiceImpl();

        LogService proxyInstance=(LogService) Proxy.newProxyInstance(
                logService.getClass().getClassLoader(),
                logService.getClass().getInterfaces(),
                new LoggingInvocationHander(logService)
        );

        proxyInstance.testMethod1();
        proxyInstance.testMethod2();
        proxyInstance.testMethod3();
    }
}

interface LogService{
    void testMethod1();
    void testMethod2();
    void testMethod3();
}

class LogServiceImpl implements LogService{
    @Log
    public void testMethod1(){
        try {
            Thread.sleep(100);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("Executing testMethod1");
    }

    @Log
    public void testMethod2(){
        try {
            Thread.sleep(150);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("Executing testMethod2");
    }

    @Override
    public void testMethod3(){
        try {
            Thread.sleep(50);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("Executing testMethod3");
    }
}
